package com.wen.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.business.mapper.ProductCategoryMapper;
import com.wen.business.mapper.ProductMapper;
import com.wen.business.service.ProductService;
import com.wen.common.enums.CodeEnum;
import com.wen.common.pojo.account.Product;
import com.wen.common.pojo.account.ProductCategory;
import com.wen.common.vo.DataVO;
import com.wen.common.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.selectList(null);
    }

    @Override
    public DataVO<ProductVO> findDate() {

        DataVO<ProductVO> dataVO = new DataVO<>();
        dataVO.setCode(CodeEnum.SUCCESS.getResultCode());
        dataVO.setMsg(CodeEnum.SUCCESS.getResultMsg());
        //查一共多少条数据
        Long count = productMapper.selectCount(null);
        dataVO.setCount(count);


        //查出所有产品list
        List<Product> list = productMapper.selectList(null);

        //new 一个list放productVO
        List<ProductVO> productVOList = new ArrayList<>();
        for (Product product : list) {
            //将一个原型产品转换成产品VO
            ProductVO productVO = new ProductVO();
            //将前面值的属性复制给后面
            BeanUtils.copyProperties(product, productVO);

            //手动赋不对应的值
            QueryWrapper<ProductCategory> wrapper;
            ProductCategory productCategory;

            wrapper = new QueryWrapper<>();
            wrapper.eq("id", product.getCategoryleveloneId());
            productCategory = productCategoryMapper.selectOne(wrapper);
            //根据id判断是否有对应的产品名称,如果没有就不用getName了
            if (productCategory != null) {
                productVO.setCategorylevelone(productCategory.getName());
            }

            wrapper = new QueryWrapper<>();
            wrapper.eq("id", product.getCategoryleveltwoId());
            productCategory = productCategoryMapper.selectOne(wrapper);
            //根据id判断是否有对应的产品名称,如果没有就不用getName了
            if (productCategory != null) {
                productVO.setCategoryleveltwo(productCategory.getName());
            }

            wrapper = new QueryWrapper<>();
            wrapper.eq("id", product.getCategorylevelthreeId());
            productCategory = productCategoryMapper.selectOne(wrapper);
            //根据id判断是否有对应的产品名称,如果没有就不用getName了
            if (productCategory != null) {
                productVO.setCategorylevelthree(productCategory.getName());
            }

            productVOList.add(productVO);
        }

        dataVO.setData(productVOList);
        return dataVO;

    }

    /**
     * 分页实现
     * @param current 起始页
     * @param PageSize 每页大小
     * @return
     */
    @Override
    public List<Product> findAllPage(int current, int PageSize) {

        Page<Product> page = new Page<>(current,PageSize);

        Page<Product> ProductPage = productMapper.selectPage(page, null);

        return ProductPage.getRecords();
    }

}
